package com.hua.ma.sang.gulimall.coupon.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.Master;
import com.hua.ma.sang.gulimall.common.utils.PageUtils;
import com.hua.ma.sang.gulimall.common.utils.Query;
import com.huaxiaofan.contentAudit.entity.AuditResult;
import com.huaxiaofan.contentAudit.service.AliYunContentAuditService;
import com.huaxiaofan.contentAudit.service.impl.AliYunContentAuditServiceImpl;
import com.huaxiaofan.sms.service.AliyunSmsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hua.ma.sang.gulimall.coupon.dao.SmsCouponDao;
import com.hua.ma.sang.gulimall.coupon.entity.SmsCouponEntity;
import com.hua.ma.sang.gulimall.coupon.service.SmsCouponService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;


@Service("smsCouponService")
@RequiredArgsConstructor
public class SmsCouponServiceImpl extends ServiceImpl<SmsCouponDao, SmsCouponEntity> implements SmsCouponService {
    private static final Logger log = LoggerFactory.getLogger(SmsCouponServiceImpl.class);


    private final AliYunContentAuditService aliYunContentAuditService;
    private final AliyunSmsService aliyunSmsService;
    private final SmsCouponDao smsCouponDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SmsCouponEntity> page = this.page(
                new Query<SmsCouponEntity>().getPage(params),
                new QueryWrapper<SmsCouponEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * 测试敏感词 以及动态数据源写数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @DS("master")
    public void saveCoupon(SmsCouponEntity smsCoupon) {
        /*Assert.notNull(smsCoupon,"缺少参数");
        List<AuditResult> auditResults = aliYunContentAuditService.TextContentAudit(smsCoupon.getCouponName());
        for (AuditResult auditResult : auditResults) {
            if (auditResult.getSuggestion().equals("block")) {
                log.error("优惠券名称包含敏感词汇 : {}",auditResult.getLabel());
            }
        }*/
        this.save(smsCoupon);
    }

    /**
     * 测试短信
     * @param id id
     * @return
     */
    @Override
    public SmsCouponEntity getCouponById(Long id) {
        boolean b = aliyunSmsService.sendSms("1");
        if (b){
            System.out.println("发送成功");
        }else{
            System.out.println("发送失败");
        }
        return new SmsCouponEntity();
    }


    /**
     * 读数据
     *
     * @param id
     */
    @Override
    @DS("slave_1")
    public SmsCouponEntity getCoupon(Long id) {
        return this.smsCouponDao.selectById(id);
    }
}